package com.zhu.pre.controller;

import com.sanploy.common.domain.dto.OrderDto;
import com.sanploy.common.vo.BaseException;
import com.zhu.pre.service.MOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletRequest;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;

/**
 * Created by zhu yingzhi on 2017/9/6.
 * @author yingzhi zhu
 * 手机订单请求路由类
 */
@RestController
@RequestMapping("/m/order")
public class MOrderController {


    @Autowired
    private MOrderService mOrderService;

    /**
     * 添加订单
     * @param orderDto 订单数据传输对象
     * @return
     * @throws NoSuchAlgorithmException
     * @throws BaseException
     * @throws IOException
     */
    @PostMapping("")
    public Object addOrder(OrderDto orderDto) throws NoSuchAlgorithmException, BaseException, IOException {
        return mOrderService.saveOrder(orderDto);
    }

    /**
     * 查找用户的订单
     * status对应状态（不传则为全部）
     * NotPay，WaitGoods ,ReceiveGoods,Cancelled,Complete
       未付款  待发货 已发货 已取消 已完成
     * @param token 请求认证
     * @param status 状态
     * @return
     * @throws BaseException 自定义异常
     */
    @GetMapping("")
    public Object findOrder(@RequestParam String token, @RequestParam(defaultValue = "") String status) throws BaseException {
        return mOrderService.findOrderByStatus(token,status);
    }

    /**
     * 通过orderId 查找的订单
     * @param token 请求认证
     * @param orderId 用户订单
     * @return
     * @throws BaseException
     */
    @GetMapping("{order_id}")
    public Object findOrderById(@RequestParam String token, @PathVariable("order_id") String orderId) throws BaseException {
        return mOrderService.findOrderById(token,orderId);
    }

    /**
     * 微信支付异步通知接口
     * @param servletRequest
     * @return
     * @throws IOException
     */
    @PostMapping("/notify")
    public Object notifyOrder(ServletRequest servletRequest) throws IOException {
        return mOrderService.payNotify(servletRequest);
    }

    /**
     * 微信支付接口
     * @param token 请求认证
     * @param orderId 订单号
     * @return
     * @throws Exception
     */
    @GetMapping("/pay")
    public Object notifyOrder(@RequestParam String token, @RequestParam String orderId) throws Exception {
        return mOrderService.payOrder(token, orderId);
    }

    /**
     * 订单取消接口
     * @param token 请求认证
     * @param orderId 订单号
     * @return
     * @throws BaseException
     */
    @GetMapping("/cancel")
    public Object cancelOrder(@RequestParam String token, @RequestParam String orderId ) throws BaseException {
        return mOrderService.cancelOrder(token, orderId);
    }

    /**
     * 确认订单接口
     * @param token 请求认证
     * @param orderId 订单号
     * @return
     * @throws BaseException
     */
    @GetMapping("/confirm")
    public Object confirmOrder(@RequestParam String token, @RequestParam String orderId ) throws BaseException {
        return mOrderService.confirmOrder(token, orderId);
    }
}
